import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { DictService } from '@app/shared/utils/dict.service'
import { ApiService } from '../../services/api.service'
import { ModalService } from '@app/shared/utils'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'
import { NetService, PageService } from '@app/core/http'
import { Router } from '@angular/router'
import {CursorService} from "@core/http/cursor.service";
import {DictPipe} from "@shared/pipes/dict.pipe";
@Component({
  selector: 'app-combo-list',
  templateUrl: './combo-list.component.html',
  styleUrls: ['./combo-list.component.scss'],
  providers: [ModalService, PageService,DictPipe]
})
export class ComboListComponent implements OnInit {
  public comboList: any[] = []
  groupname ='';
  groupcode ='';
  productcategory = '';
  salestate = '';
  public productTypes = [
    { value: '1', label: '理财产品' },
    { value: '2', label: '基金' },
    { value: '3', label: '期货' },
    { value: '4', label: '债劵' },
    { value: '5', label: '股票' },
    { value: '6', label: '贵金属' }
  ]
  constructor(
    public dictService: DictService,
    public cursorService: CursorService,
    private apiService: ApiService,
    private fb: FormBuilder,
    public modal: ModalService,
    public pageService: PageService,
    private message: QlMessageService,
    private router: Router,
    private dictPipe:DictPipe
  ) {}

  ngOnInit() {
   this.onRefresh()
    console.log('sale_state：'+this.dictPipe.transform('0','sale_state'))
  }
  public onRefresh() {
    this.cursorService.limit = 10;
    let sql='select *'
    if(this.groupname!=''){
      this.cursorService.query().eq('groupname',this.groupname)

      // sql= this.transform('groupname',this.groupname ,sql)
    }
    if(this.groupcode!=''){
      this.cursorService.query().fromString("select * where groupcode  ="+"'*"+this.groupcode+"*'")
      // this.cursorService.query().eq('groupcode',this.groupcode)
      // sql= this.transform('groupcode',this.groupcode ,sql)
    }
    if(this.productcategory!=''){
      this.cursorService.query().eq('productcategory',this.productcategory)
      // sql= this.transform('productcategory',this.productcategory ,sql)
    }
    if(this.salestate!=''){
      this.cursorService.query().eq('salestate',this.salestate)
      // sql= this.transform('salestate',this.salestate ,sql)
    }
    // this.cursorService.statement =sql;
    console.log('sql:'+JSON.stringify(this.cursorService))
    this.apiService.getcomboList(this.cursorService).subscribe(data => {
      this.comboList = data;
    })
    this.cursorService.query({reset:true})
  }
  transform(key:string,value:string,sql:string){
    if(sql != 'select *'){
      sql += ' and ' + key +" = " + "'*" + value + "*'"
    }
    else{
      sql += " where " + key +" = " + "'*" + value + "*'";
    }
    return sql
  }
  public delFun(groupcode) {
    this.modal
      .open({
        title: '确认删除',
        component: ModalConfirmComponent
      })
      .subscribe(() => {
        this.apiService.delGroup(groupcode).subscribe(data => {
          this.message.success('删除成功')
          this.onRefresh()
        })
      })
  }
  public pull(groupcode){
    let params ={	"status":1}
    this.modal
      .open({
        title: '确认上架',
        component: ModalConfirmComponent
      })
      .subscribe(() => {
        this.apiService.updateGroup(params,groupcode).subscribe(data => {
          this.message.success('上架成功')
        })
      })
  }
  public stop(groupcode){
    let params ={	"status":0}
    this.modal
      .open({
        title: '确认停用',
        component: ModalConfirmComponent
      })
      .subscribe(() => {
        this.apiService.updateGroup(params,groupcode).subscribe(data => {
          this.message.success('停用成功')
        })
      })
  }
}
